#include "SDL/SDL.h"

#include "video.h"
#include "object.h"
#include "plcsystem.h"
#include "objectsystem.h"
#include "paletteNEWER.h"
#include "zoneNEWER.h"
#include "draw.h"
#include "plc.h"
#include "camera.h"
#include "sprite.h"
#include "sonic.h"
#include "joypad.h"
#include "collision.h"
#include <stdio.h>


//extern zone currentZone;
extern object* player;
extern object* objects;
extern layerpalette backgroundpal;
extern layerpalette foregroundpal;

extern byte v_16x16[];

word *vRamptr=getvRam(); 
word *fgRamptr=getfgRam(); 
long *v_hscrolltablebuf=cameraGetHScrollTable();
byte *v_spritetablebuf=(byte*)getSpriteTableBuf();
extern byte v_sgfx_buffer[];
extern long v_screenposx;
extern long v_screenposy;

bool keys[0xFFFF];
extern byte v_jpadhold1;
int mousex,mousey; 
bool quit=false;

void PrintTiles();
void DrawSixteens();

byte v_zone;
    
void DrawScene();
SDL_Surface *screen;
char coordstr[25];
char tileinfo[50];
char tile16info[50];
char joypadinfo[50];

int main( int argc, char** argv ) 
{ 
    initVideo();

    pcPatterns();
    //pcPalettes();
    pcObjects();
    pcObjectMethods();
    //pcLevels();
    pcLevelBounds();
    pcScrolling();
    pcSonic();
    pcStartLocations();

    v_zone=0;

    
    LevelSizeLoad(v_zone);
    //LevelDataLoad(v_zone);
    LevelDataLoad();
    ColIndexLoad(v_zone);
    
    SDL_Event event;
    SDL_Init( SDL_INIT_VIDEO ); 
    screen = SDL_SetVideoMode( 960, 720, 32, SDL_HWSURFACE|SDL_SRCCOLORKEY );        
    
    ClearPLCs();
    AddPLC(4); //index of GHZ (TODO: make this id come from levelheaders
    RunPLCs();
        
    InitTiles();
    vRamToTiles();
    
     #define id_SonicPlayer 1
     objects[0].ID=1;
     ExecuteObjects();
     BuildSprites();

     
     LoadTilesFromStart(v_screenposx,(v_screenposy&0xFFFF0000)>>16);
     bgRamWithTilesToSurface(screen);
     fgRamWithTilesToSurface(screen);

     SDL_Flip(screen);


    
    //While the user hasn't quit 
    while( quit == false ) 
    { 
        //if ( v_jpadhold2 ) 
        //{  
        //     objects[0].X += 8;
        //}
	//if ( keys[SDLK_LEFT] )
        //{ 
        //     objects[0].X -= 8;
       // }
	//if ( keys[SDLK_DOWN] ) 
        //{ 
         //    objects[0].Y += 8;
        //}
	//if ( keys[SDLK_UP] ) 
        //{      
        //     objects[0].Y -= 8;
       // }
        ExecuteObjects();
        BuildSprites();
        DrawScene();
        
        //While there's an event to handle 
        while( SDL_PollEvent( &event ) ) 
        { 
              //DisplaySprite(objects[0]);
                
               if( event.type == SDL_QUIT ) 
               { 
                   //Quit the program 
                   quit = true; 
               }
               if ( event.type == SDL_KEYDOWN )
               {
                   if ( event.key.keysym.sym == SDLK_ESCAPE ) { quit=true; }                   else { keys[event.key.keysym.sym]=true; }
               }
     	       if ( event.type == SDL_KEYUP )
	              keys[event.key.keysym.sym]=false;
	       if ( event.type == SDL_MOUSEMOTION )
	       {
                  mousex=event.motion.x;
                  mousey=event.motion.y;
                  
                  sprintf(coordstr, "MOUSEX %i MOUSEY %i", mousex, mousey);
                  sprintf(tileinfo, "CURRENT 8 BY 8 %i", fgRamptr[(mousex/24)+(((mousey)/24)*64)]);
                  long sixteenaddress=(long)DrawBlocks(v_screenposx, (v_screenposy&0xFFFF0000)>>16,0x4000, mousey/3,mousex/3,0,0);
                  //sprintf(tile16info, "CURRENT 16 BY 16 %04X", (sixteenaddress-(int)currentZone.v_16x16));
                  sprintf(tile16info, "CURRENT 16 BY 16 %04X", (sixteenaddress-(int)v_16x16));
               } 
           }
      }


    KillTiles();
    SDL_Quit(); 
    
    return 0; 
} 

void DrawScene()
{
     DeformLayers(v_zone);

     /* vblank code */
     ReadJoypads(keys);
     UpdateHScrollTable(v_hscrolltablebuf);
     UpdateSpriteTable(v_spritetablebuf);
     writeVRAM(v_sgfx_buffer,0x2E0,0xF000/*vram_sonic*/);
     
     /*------------*/


     LoadTilesFromStart(v_screenposx,(v_screenposy&0xFFFF0000)>>16);
     
     char *_joypadinfo=joypadinfo;
     for (signed int pn=-2; pn<2; pn++)
     {
	     byte n=*((&v_jpadhold1)+pn);
	     unsigned int i = 1 << (sizeof(n) * 8 - 1);
	     while (i > 0) 
	     {
		if (n & i) { *(_joypadinfo++)='1'; }
		else       { *(_joypadinfo++)='0'; }
		i >>= 1;
	     }
             (*_joypadinfo++)=' ';
     }


     DrawText(coordstr,25,10,25);
     DrawText(tileinfo,25,10,26);
     DrawText(tile16info,26,10,27);
     DrawText(joypadinfo,45,10,28);

     //DrawSixteens();
     vRamToTiles();   
     
     bgRamWithTilesToSurface(screen);
     fgRamWithTilesToSurface(screen);
     spritesToSurface(screen); 
 
     SDL_Rect hilight;     hilight.x=(mousex/3)-((mousex/3)&0x0F);     hilight.y=(mousey/3)-((mousey/3)&0x0F);
     hilight.w=16;
     hilight.h=16;
     SDL_DrawRect(screen, hilight.x, hilight.y, hilight.w, hilight.h, 0x00FFFFFF);
     SDL_Flip(screen);
}

void DrawSixteens()
{
/*
    ////printf("16x16 tiles: \n");
    
    /*\\for (int lp=0; lp<6144/32; lp++)
    {
       word cur16val=(currentZone.v_16x16[(lp*8)]<<8)+(currentZone.v_16x16[(lp*8)+1]);
        
         int xflip=(cur16val&0x800)>>11; //x flip flag
         int yflip=(cur16val&0x1000)>>12; //y flip flag
         int line=(cur16val&0x6000)>>13;
                         
         cur16val=cur16val&0x7FF+(line<<13);
         
         fgTile(0,0,((lp%32)*2)+((lp/32)*128),cur16val);
         
         
         //cur16val=cur16val*0x10;
        ////printf("%02X", currentZone.v_16x16[(lp*8)]);
        ////printf("%02X", currentZone.v_16x16[(lp*8)+1]);
        
        
        
        ////printf(" %02X", currentZone.v_16x16[(lp*8)+2]);
        ////printf("%02X", currentZone.v_16x16[(lp*8)+3]);
        
        cur16val=(currentZone.v_16x16[(lp*8)+2]<<8)+(currentZone.v_16x16[(lp*8)+3]);
        
         xflip=(cur16val&0x800)>>11; //x flip flag
         yflip=(cur16val&0x1000)>>12; //y flip flag
         line=(cur16val&0x6000)>>13;
                        
         cur16val=cur16val&0x7FF+(line<<13);
         
         fgTile(0,0,((lp%32)*2)+((lp/32)*128)+1,cur16val);
        ////printf ("\n%02X", currentZone.v_16x16[(lp*8)+4]);
        ////printf ("%02X", currentZone.v_16x16[(lp*8)+5]);
        
        cur16val=(currentZone.v_16x16[(lp*8)+4]<<8)+(currentZone.v_16x16[(lp*8)+5]);
        
         xflip=(cur16val&0x800)>>11; //x flip flag
         yflip=(cur16val&0x1000)>>12; //y flip flag
         line=(cur16val&0x6000)>>13;
                        
         cur16val=cur16val&0x7FF+(line<<13);
         fgTile(0,0,((lp%32)*2)+((lp/32)*128)+64,cur16val);
         
         cur16val=(currentZone.v_16x16[(lp*8)+6]<<8)+(currentZone.v_16x16[(lp*8)+7]);
        
         xflip=(cur16val&0x800)>>11; //x flip flag
         yflip=(cur16val&0x1000)>>12; //y flip flag
         line=(cur16val&0x6000)>>13;
                        
         cur16val=cur16val&0x7FF+(line<<13);
         fgTile(0,0,((lp%32)*2)+((lp/32)*128)+65,cur16val);
         
         
         
         ////printf(" %02X", currentZone.v_16x16[(lp*8)+6]);
        ////printf("%02X", currentZone.v_16x16[(lp*8)+7]);
        ////printf ("\n\n");
    }
    
        for (int index256=0x2C; index256 < 0x2D; index256++)
        {
            //printf("tiles mapped by 256x256 #%i:\n", index256);
            //for each 16x16 mapping in 256
            for (int cur16y=0; cur16y<16; cur16y++)
            {
                   for (int cur16x=0; cur16x<16; cur16x++)
                   {
                       
                       int index=(index256*256)+cur16x+(cur16y*16);
                       
                       word lp=(currentZone.v_256x256[(index*2)+1]+(currentZone.v_256x256[(index*2)+0]<<8))&0x3FF;
                       //printf("%04X ", lp);
                       
                       word cur16val=(currentZone.v_16x16[(lp*8)]<<8)+(currentZone.v_16x16[(lp*8)+1]);
        
                       int xflip=(cur16val&0x800)>>11; //x flip flag
                       int yflip=(cur16val&0x1000)>>12; //y flip flag
                       int line=(cur16val&0x6000)>>13;
                         
                       
                       
                       cur16val=cur16val&0x7FF+(line<<13);
         
                       //int lp2=lp%256;
                      //fgTile(0,0,(((cur16x)*2)+((cur16y)*128)),cur16val);
         
         
                     //cur16val=cur16val*0x10;
                    ////printf("%02X", currentZone.v_16x16[(lp*8)]);
                    ////printf("%02X", currentZone.v_16x16[(lp*8)+1]);
                    
                    
                    
                    ////printf(" %02X", currentZone.v_16x16[(lp*8)+2]);
                    ////printf("%02X", currentZone.v_16x16[(lp*8)+3]);
                    
                    cur16val=(currentZone.v_16x16[(lp*8)+2]<<8)+(currentZone.v_16x16[(lp*8)+3]);
                    
                     xflip=(cur16val&0x800)>>11; //x flip flag
                     yflip=(cur16val&0x1000)>>12; //y flip flag
                     line=(cur16val&0x6000)>>13;
                                    
                     cur16val=cur16val&0x7FF+(line<<13);
                     
                     //lp2=lp%256;
                     fgTile(0,0,(((cur16x)*2)+((cur16y)*128))+1,cur16val);
                    ////printf ("\n%02X", currentZone.v_16x16[(lp*8)+4]);
                    ////printf ("%02X", currentZone.v_16x16[(lp*8)+5]);
                    
                    cur16val=(currentZone.v_16x16[(lp*8)+4]<<8)+(currentZone.v_16x16[(lp*8)+5]);
                    
                     xflip=(cur16val&0x800)>>11; //x flip flag
                     yflip=(cur16val&0x1000)>>12; //y flip flag
                     line=(cur16val&0x6000)>>13;
                                    
                     cur16val=cur16val&0x7FF+(line<<13);
                     
                     //lp2=lp%256;
                     fgTile(0,0,(((cur16x)*2)+((cur16y)*128))+64,cur16val);
                     
                     cur16val=(currentZone.v_16x16[(lp*8)+6]<<8)+(currentZone.v_16x16[(lp*8)+7]);
                    
                     xflip=(cur16val&0x800)>>11; //x flip flag
                     yflip=(cur16val&0x1000)>>12; //y flip flag
                     line=(cur16val&0x6000)>>13;
                                    
                     cur16val=cur16val&0x7FF+(line<<13);
                     //lp2=lp%256;
                     fgTile(0,0,(((cur16x)*2)+((cur16y)*128))+65,cur16val);
         
         /*
                       word cur16val=(currentZone.v_16x16[index*8]<<8)+currentZone.v_16x16[(index*8)+1];
                       ////printf("cur16val: %i\n", cur16val);
                       
                       ////printf("index: %i\n", index);
                       
                       //int index=currentZone.v_256x256[index256*64+(cur16y*8)+cur16x];//16x16 tile at index [cur16x,cur16y] of 256x256s [index256]        
                       //int index2=currentZone.v_256x256[index256*64+(cur16y*8)+cur16x+1];
                       //int index3=currentZone.v_256x256[index256*64+((cur16y+1)*8)+cur16x];         
                       //int index4=currentZone.v_256x256[index256*64+((cur16y+1)*8)+cur16x+1];
               
                        int xflip=(cur16val&0x800)>>11; //x flip flag
                        int yflip=(cur16val&0x1000)>>12; //y flip flag
                        
                        cur16val=cur16val&0x7FF;
                        cur16val=cur16val*0x10;
                        
                        ////printf("xflip: %i, yflip: %i\n", xflip, yflip);
                        if (xflip==0 && yflip==0) {
                             
                            for (int a=1; a>=0; a--)
                            {
                                //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                            
                            }
                        }
                        
                        if (yflip==1) { b=7-b; }
                        if (xflip==0 && yflip==1) 
                        {  
                            for (int a=1; a>=0; a--)
                               //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] );                        
                        }
                        if (xflip==1) {
                             
                             word temp=vRamptr[(1+(b*2)) +  (cur16val)];
                             word t1=temp&0xF000;
                             word t2=temp&0x0F00;
                             word t3=temp&0x00F0;
                             word t4=temp&0x000F;
                             word temp2=vRamptr[((b*2)) +  (cur16val)];
                             word t5=temp2&0xF000;
                             word t6=temp2&0x0F00;
                             word t7=temp2&0x00F0;
                             word t8=temp2&0x000F;
                             word tempnew=(t8<<12)+(t7<<4)+(t6>>4)+(t5>>12);
                             word temp2new=(t4<<12)+(t3<<4)+(t2>>4)+(t1>>12);
                             
                             //printf("%04X", tempnew);
                             //printf("%04X", temp2new);
                        }
                        if (yflip==1) { b=7-b; }

                        
                        index=(index256*256)+cur16x+(cur16y*16);
                        
                        cur16val=(currentZone.v_16x16[(index*8)+2]<<8)+currentZone.v_16x16[(index*8)+3]; 
                        
                        xflip=(cur16val&0x800)>>11; //x flip flag
                        yflip=(cur16val&0x1000)>>12; //y flip flag   
                        
                        cur16val=cur16val&0x7FF;
                        cur16val=cur16val*0x10;
                        
                         if (xflip==0 && yflip==0) {
                             
                        for (int a=1; a>=0; a--)
                            ////printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                        
                        }
                        
                        if (yflip==1) { b=7-b; }
                        if (xflip==1) {
                             
                            word temp=vRamptr[(1+(b*2)) +  (cur16val)];
                             word t1=temp&0xF000;
                             word t2=temp&0x0F00;
                             word t3=temp&0x00F0;
                             word t4=temp&0x000F;
                             word temp2=vRamptr[((b*2)) +  (cur16val)];                       
                             word t5=temp2&0xF000;
                             word t6=temp2&0x0F00;
                             word t7=temp2&0x00F0;
                             word t8=temp2&0x000F;
                             word tempnew=(t8<<12)+(t7<<4)+(t6>>4)+(t5>>12);
                             word temp2new=(t4<<12)+(t3<<4)+(t2>>4)+(t1>>12);
                             
                             //printf("%04X", tempnew);
                             //printf("%04X", temp2new);
                        }
                        if (yflip==1) { b=7-b; }
                        
                        if (xflip==0 && yflip==1) 
                        {  
                            for (int a=1; a>=0; a--)
                                //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16                        
                        }
                                            
                        
                        //printf(" ");                                                                  //seperate by 16
    
                    }
                    //printf("\n");
                }
               for (int b=0; b<8; b++)
               {
                   for (int cur16x=0; cur16x<16; cur16x++)
                   {
                       int index=(index256*256)+cur16x+(cur16y*16);
                       //index=index/8;
                       word cur16val=(currentZone.v_16x16[(index*8)+4]<<8)+currentZone.v_16x16[(index*8)+5];
                   
                       //int index=currentZone.v_256x256[index256*64+(cur16y*8)+cur16x];//16x16 tile at index [cur16x,cur16y] of 256x256s [index256]        
                       //int index2=currentZone.v_256x256[index256*64+(cur16y*8)+cur16x+1];
                       //int index3=currentZone.v_256x256[index256*64+((cur16y+1)*8)+cur16x];         
                       //int index4=currentZone.v_256x256[index256*64+((cur16y+1)*8)+cur16x+1];
                       
                        
                        int xflip=(cur16val&0x800)>>11; //x flip flag
                        int yflip=(cur16val&0x1000)>>12; //y flip flag    
                        
                        cur16val=cur16val&0x7FF;
                        cur16val=cur16val*0x10;
                        
                         if (xflip==0 && yflip==0) {
                             
                        for (int a=1; a>=0; a--)
                            //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                        
                        }
                        
                        if (yflip==1) { b=7-b; }
                        if (xflip==0 && yflip==1) 
                        {  
                            for (int a=1; a>=0; a--)
                            //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                        
                        }       
   
                        if (xflip==1) {
                             
                             word temp=vRamptr[(1+(b*2)) +  (cur16val)];
                             word t1=temp&0xF000;
                             word t2=temp&0x0F00;
                             word t3=temp&0x00F0;
                             word t4=temp&0x000F;
                             word temp2=vRamptr[((b*2)) +  (cur16val)];
                             word t5=temp2&0xF000;
                             word t6=temp2&0x0F00;
                             word t7=temp2&0x00F0;
                             word t8=temp2&0x000F;
                             word tempnew=(t8<<12)+(t7<<4)+(t6>>4)+(t5>>12);
                             word temp2new=(t4<<12)+(t3<<4)+(t2>>4)+(t1>>12);
                             
                             //printf("%04X", tempnew);
                             //printf("%04X", temp2new);
                        }
                        if (yflip==1) { b=7-b; }
                                                
                        cur16val=(currentZone.v_16x16[(index*8)+6]<<8)+currentZone.v_16x16[(index*8)+7];
                        
                        xflip=(cur16val&0x800)>>11; //x flip flag
                        yflip=(cur16val&0x1000)>>12; //y flip flag                    
     
                        cur16val=cur16val&0x7FF;
                        cur16val=cur16val*0x10;
                        
                        if (xflip==0 && yflip==0) {
                             
                        for (int a=1; a>=0; a--)
                           //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                         
                        }
                        
                        if (yflip==) { b=7-b; }
                        if (xflip==0 && yflip==1) 
                        {  
                            for (int a=1; a>=0; a--)
                                 //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                        
                        }
                        if (xflip==1) {
                             
                             word temp=vRamptr[(1+(b*2)) +  (cur16val)];
                             word t1=temp&0xF000;
                             word t2=temp&0x0F00;
                             word t3=temp&0x00F0;
                             word t4=temp&0x000F;
                             word temp2=vRamptr[((b*2)) +  (cur16val)];                             
                             word t5=temp2&0xF000;
                             word t6=temp2&0x0F00;
                             word t7=temp2&0x00F0;
                             word t8=temp2&0x000F;
                             word tempnew=(t8<<12)+(t7<<4)+(t6>>4)+(t5>>12);
                             word temp2new=(t4<<12)+(t3<<4)+(t2>>4)+(t1>>12);
                             
                             //printf("%04X", tempnew);
                             //printf("%04X", temp2new);
                        }
                        if (yflip==1) { b=7-b; }
                        //printf(" ");                                                 //seperate by 16
    
                    }
                    //printf("\n");
                }
                //printf("\n");
            
          }    
       }
           
       
                }
                //printf("\n");
            }
        }
*/
}
    
void PrintTiles()
{
    /*//printf("16x16 tiles: \n");
    
    for (int lp=0; lp<6144/8; lp++)
    {
        //printf("%02X", currentZone.v_16x16[(lp*8)]);
        //printf("%02X", currentZone.v_16x16[(lp*8)+1]);
        
        //printf(" %02X", currentZone.v_16x16[(lp*8)+2]);
        //printf("%02X", currentZone.v_16x16[(lp*8)+3]);
        
        //printf ("\n%02X", currentZone.v_16x16[(lp*8)+4]);
        //printf ("%02X", currentZone.v_16x16[(lp*8)+5]);
        
         //printf(" %02X", currentZone.v_16x16[(lp*8)+6]);
        //printf("%02X", currentZone.v_16x16[(lp*8)+7]);
        //printf ("\n\n");
    } 
    
      for (int index256=0; index256 < 0xFF; index256++)
        {
            //printf("tiles mapped by 256x256 #%i:\n", index256);
            //for each 16x16 mapping in 256
          for (int cur16y=0; cur16y<16; cur16y++)
          {
              //int index=cur16x
               for (int b=0; b<8; b++)
               {
                   for (int cur16x=0; cur16x<16; cur16x++)
                   {
                       
                       int index=(index256*256)+cur16x+(cur16y*16);
                       word cur16val=(currentZone.v_16x16[index*8]<<8)+currentZone.v_16x16[(index*8)+1];
                       ////printf("cur16val: %i\n", cur16val);
                       
                       ////printf("index: %i\n", index);
                       
                       //int index=currentZone.v_256x256[index256*64+(cur16y*8)+cur16x];//16x16 tile at index [cur16x,cur16y] of 256x256s [index256]        
                       //int index2=currentZone.v_256x256[index256*64+(cur16y*8)+cur16x+1];
                       //int index3=currentZone.v_256x256[index256*64+((cur16y+1)*8)+cur16x];         
                       //int index4=currentZone.v_256x256[index256*64+((cur16y+1)*8)+cur16x+1];
               
                        int xflip=(cur16val&0x800)>>11; //x flip flag
                        int yflip=(cur16val&0x1000)>>12; //y flip flag
                        
                        cur16val=cur16val&0x7FF;
                        cur16val=cur16val*0x10;
                        
                        ////printf("xflip: %i, yflip: %i\n", xflip, yflip);
                        if (xflip==0 && yflip==0) {
                             
                            for (int a=1; a>=0; a--)
                            {
                                //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                            
                            }
                        }
                        
                        if (yflip==1) { b=7-b; }
                        if (xflip==0 && yflip==1) 
                        {  
                            for (int a=1; a>=0; a--)
                            {   
                                //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] );              
                            }          
                        }
                        if (xflip==1) {
                             
                             word temp=vRamptr[(1+(b*2)) +  (cur16val)];
                             word t1=temp&0xF000;
                             word t2=temp&0x0F00;
                             word t3=temp&0x00F0;
                             word t4=temp&0x000F;
                             word temp2=vRamptr[((b*2)) +  (cur16val)];
                             word t5=temp2&0xF000;
                             word t6=temp2&0x0F00;
                             word t7=temp2&0x00F0;
                             word t8=temp2&0x000F;
                             word tempnew=(t8<<12)+(t7<<4)+(t6>>4)+(t5>>12);
                             word temp2new=(t4<<12)+(t3<<4)+(t2>>4)+(t1>>12);
                             
                             //printf("%04X", tempnew);
                             //printf("%04X", temp2new);
                        }
                        if (yflip==1) { b=7-b; }

                        
                        index=(index256*256)+cur16x+(cur16y*16);
                        
                        cur16val=(currentZone.v_16x16[(index*8)+2]<<8)+currentZone.v_16x16[(index*8)+3]; 
                        
                        xflip=(cur16val&0x800)>>11; //x flip flag
                        yflip=(cur16val&0x1000)>>12; //y flip flag   
                        
                        cur16val=cur16val&0x7FF;
                        cur16val=cur16val*0x10;
                        
                        if (xflip==0 && yflip==0) 
                        {
                             
                        for (int a=1; a>=0; a--)
                        {
                            //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                        }
                        
                        if (yflip==1) { b=7-b; }
                        if (xflip==1) 
                        {
                             
                            word temp=vRamptr[(1+(b*2)) +  (cur16val)];
                             word t1=temp&0xF000;
                             word t2=temp&0x0F00;
                             word t3=temp&0x00F0;
                             word t4=temp&0x000F;
                             word temp2=vRamptr[((b*2)) +  (cur16val)];                       
                             word t5=temp2&0xF000;
                             word t6=temp2&0x0F00;
                             word t7=temp2&0x00F0;
                             word t8=temp2&0x000F;
                             word tempnew=(t8<<12)+(t7<<4)+(t6>>4)+(t5>>12);
                             word temp2new=(t4<<12)+(t3<<4)+(t2>>4)+(t1>>12);
                             
                             //printf("%04X", tempnew);
                             //printf("%04X", temp2new);
                        }
                        if (yflip==1) { b=7-b; }
                        
                        if (xflip==0 && yflip==1) 
                        {  
                            for (int a=1; a>=0; a--)
                            {
                                //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16    
                            }                    
                        }
                                            
                        
                        //printf(" ");                                                                  //seperate by 16
    
                    }
                    //printf("\n");
                }
               for (int b=0; b<8; b++)
               {
                   for (int cur16x=0; cur16x<16; cur16x++)
                   {
                       int index=(index256*256)+cur16x+(cur16y*16);
                       //index=index/8;
                       word cur16val=(currentZone.v_16x16[(index*8)+4]<<8)+currentZone.v_16x16[(index*8)+5];
                   
                       //int index=currentZone.v_256x256[index256*64+(cur16y*8)+cur16x];//16x16 tile at index [cur16x,cur16y] of 256x256s [index256]        
                       //int index2=currentZone.v_256x256[index256*64+(cur16y*8)+cur16x+1];
                       //int index3=currentZone.v_256x256[index256*64+((cur16y+1)*8)+cur16x];         
                       //int index4=currentZone.v_256x256[index256*64+((cur16y+1)*8)+cur16x+1];
                       
                        
                        int xflip=(cur16val&0x800)>>11; //x flip flag
                        int yflip=(cur16val&0x1000)>>12; //y flip flag    
                        
                        cur16val=cur16val&0x7FF;
                        cur16val=cur16val*0x10;
                        
                        if (xflip==0 && yflip==0) {
                             
                        for (int a=1; a>=0; a--)
                        {
                            //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                        }
                        
                        if (yflip==1) { b=7-b; }
                        if (xflip==0 && yflip==1) 
                        {  
                            for (int a=1; a>=0; a--)
                            {
                            //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                            }
                        
                        }       
   
                        if (xflip==1) 
                        {
                             
                             word temp=vRamptr[(1+(b*2)) +  (cur16val)];
                             word t1=temp&0xF000;
                             word t2=temp&0x0F00;
                             word t3=temp&0x00F0;
                             word t4=temp&0x000F;
                             word temp2=vRamptr[((b*2)) +  (cur16val)];
                             word t5=temp2&0xF000;
                             word t6=temp2&0x0F00;
                             word t7=temp2&0x00F0;
                             word t8=temp2&0x000F;
                             word tempnew=(t8<<12)+(t7<<4)+(t6>>4)+(t5>>12);
                             word temp2new=(t4<<12)+(t3<<4)+(t2>>4)+(t1>>12);
                             
                             //printf("%04X", tempnew);
                             //printf("%04X", temp2new);
                        }
                        if (yflip==1) { b=7-b; }
                                                
                        cur16val=(currentZone.v_16x16[(index*8)+6]<<8)+currentZone.v_16x16[(index*8)+7];
                        
                        xflip=(cur16val&0x800)>>11; //x flip flag
                        yflip=(cur16val&0x1000)>>12; //y flip flag                    
     
                        cur16val=cur16val&0x7FF;
                        cur16val=cur16val*0x10;
                        
                        if (xflip==0 && yflip==0) 
                        {     
                            for (int a=1; a>=0; a--)
                            {
                               //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                            }
                        }
                        
                        if (yflip==1) { b=7-b; }
                        if (xflip==0 && yflip==1) 
                        {  
                            for (int a=1; a>=0; a--)
                            {
                                 //printf("%04X", vRamptr[(a + (b*2)) +  (cur16val)] ); //first 8x8 in current 16
                            }
                        }
                        if (xflip==1) 
                        {
                             
                             word temp=vRamptr[(1+(b*2)) +  (cur16val)];
                             word t1=temp&0xF000;
                             word t2=temp&0x0F00;
                             word t3=temp&0x00F0;
                             word t4=temp&0x000F;
                             word temp2=vRamptr[((b*2)) +  (cur16val)];                             
                             word t5=temp2&0xF000;
                             word t6=temp2&0x0F00;
                             word t7=temp2&0x00F0;
                             word t8=temp2&0x000F;
                             word tempnew=(t8<<12)+(t7<<4)+(t6>>4)+(t5>>12);
                             word temp2new=(t4<<12)+(t3<<4)+(t2>>4)+(t1>>12);
                             
                             //printf("%04X", tempnew);
                             //printf("%04X", temp2new);
                        }
                        if (yflip==1) { b=7-b; }
                        //printf(" ");                                                 //seperate by 16
    
                    }
                    //printf("\n");
                }
                //printf("\n");
            
          }    
       }            
    }

}*/
}


